﻿using System;
using System.Collections.Generic;
using System.Linq;
using AccountManagement.DataAccess;

namespace AccountManagement.BussinessObjects
{
    class AccountsBO
    {
        private readonly LinqHelper<AccountsDataContext> _helper = new LinqHelper<AccountsDataContext>();

        /// <summary>
        /// Get all Incoming
        /// </summary>
        /// <returns>List Incoming</returns>
        public List<tblAccount> GetAll()
        {
            try
            {
                return _helper.GetTable<tblAccount>().ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public int Update(tblAccount objAccount, decimal amount)
        {
            try
            {
                _helper.BeginTransaction();
                _helper.Update(objAccount, t => t.SoTK == objAccount.SoTK);

                if (objAccount.TKCha != null)
                {
                    var objTkCha = _helper.GetOne<tblAccount>(t => t.SoTK == objAccount.TKCha);
                    objTkCha.SoDu = objTkCha.SoDu + amount;
                    Update(objTkCha, amount);
                }
                _helper.Transaction.CommitAndDispose();
                return 0;
            }
            catch (Exception)
            {
                _helper.Transaction.RollbackAndDispose();
                return Constants.Errors.UNKNOW;
            }
        }
    }
}
